<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/9
 * Time: 22:10
 */
header('Content-type: text/json;charset=utf-8');
$_POST ? $data = $_POST : $data = $_GET;
$id = $data['id'];

if (!$id) {
    exit();
}
require_once "config.php";
require_once "MyPDO.class.php";
$db = MyPDO::getInstance($host, $user, $pass, $dbName, 'utf8');

$admin = $_COOKIE['user'];
if(!$admin){
  exit();
}
switch ($id) {
    case "getBackupListNew":
        $search = json_decode($data['search'], true);
        $searchName = $search['search'];
        $day = $data['day'];
        //$babyId = $data['babyId'];
        $sql = "   `up_user` = '$admin'";
        if ($data['name']) {
            $name = $data['name'];
            $sql .= " AND  (`name` LIKE '%$name%' OR `text1` LIKE '%$name%' OR `text2` LIKE '%$name%') ";
        }
        $total = $db->getCount('backup_new', '`id`', $sql);
        $pn = ($search['pn'] - 1) * 20;
        $sql .= " ORDER BY `id` DESC LIMIT $pn,20";

        $arr = $db->query("backup_new", $sql);
        foreach ($arr as $k => $v) {
            $arr[$k]['img1'] = json_decode($v['img1'], 1);
            $arr[$k]['img2'] = json_decode($v['img2'], 1);
            $arr[$k]['babyList'] = json_decode($v['babyList'], 1);
        }
        $json['status'] = "success";
        $json['msg'] = "ok";
        $json['total'] = $total;
        $json['data'] = $arr;
        echo json_encode($json);
        break;
    case "backupToNew":
        $babyId = $data['toID'];
        $day = $data['time'];
        $jsonData = json_decode($data['data'], 1);
        foreach ($jsonData as $k => $v) {
            $update['name'] = $v['name'];
            $update['day'] = $day;
            $update['babyId'] = $babyId;
            $update['text1'] = $v['text1'];
            $update['img1'] = json_encode($v['img1'], JSON_UNESCAPED_UNICODE);
            $update['text2'] = $v['text2'];
            $update['img2'] = json_encode($v['img2'], JSON_UNESCAPED_UNICODE);
            $update['addTime'] = time();
            $update['up_user'] = $v['user'];
            $update['admin'] = $v['admin'];
            $update['admin_time'] = $v['adminTime'];
            $update['status'] = $v['backupStatus'];
            $update['day2'] = mt_rand(1, 7);
            $db->insert('backup_new', $update);
        }
        break;
    case "getBackupList":
        $search = json_decode($data['search'], true);
        $searchName = $search['search'];
        $day = $data['day'];
        $babyId = $data['babyId'];
        $sql = " `day` = '$day' AND `babyId` = '$babyId'";
        if ($data['name']) {
            $name = $data['name'];
            $sql .= " AND  (`name` LIKE '%$name%' OR `text1` LIKE '%$name%' OR `text2` LIKE '%$name%') ";
        }
        $total = $db->getCount('backup_new', '`id`', $sql);
        $pn = ($search['pn'] - 1) * 50;
        $sql .= " ORDER BY `id` DESC LIMIT $pn,50";

        $arr = $db->query("backup_new", $sql);
        foreach ($arr as $k => $v) {
            $arr[$k]['img1'] = json_decode($v['img1'], 1);
            $arr[$k]['img2'] = json_decode($v['img2'], 1);
            $arr[$k]['babyList'] = json_decode($v['babyList'], 1);
        }
        $json['status'] = "success";
        $json['msg'] = "ok";
        $json['total'] = $total;
        $json['data'] = $arr;
        echo json_encode($json);
        break;
    case "copyList":
        $babyId = $data['babyId'];
        $toId = $data['toId'];
        $name = $data['toBabyName'];
        $arr = $db->query("backup_new", "`babyId` = '$babyId'");
        $a = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31);
        shuffle($a);
        foreach ($arr as $k => $v) {
            $update = $v;
            $update['day'] = $a[$v['day'] - 1];
            unset($update['id']);
            unset($update['name']);
            $update['day2'] = mt_rand(1, 7);
            $update['name'] = $name;
            $update['babyId'] = $toId;
            $db->insert('backup_new', $update);
        }
        $json['status'] = "success";
        $json['msg'] = "ok";
        echo json_encode($json);
        break;
}